package com.hgshop.yudongyang.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.hgshop.yudongyang.domain.Brand;
import com.hgshop.yudongyang.service.BrandService;

@Controller
@RequestMapping("brand")
public class BrandController {

	
	@Reference
	BrandService brandService;
	@Value("${file.path}")
	String path;
	
	
	
	@RequestMapping("list")
	public String list(Model model,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize) {
		PageInfo<Brand> info = brandService.list(pageNum, pageSize);
		model.addAttribute("info", info);
		return "brand/list";
		
	}
	
	@GetMapping("add")
	public String add() {
		
		return "brand/addBrand";
	}
	@ResponseBody
	@PostMapping("add")
	public boolean add(Brand brand,MultipartFile file) {
		if(file != null && file.getSize()>0) {
			
			
			String fileName=UUID.randomUUID().toString()+file.getOriginalFilename();
			File file2 = new File(path, fileName);
			try {
				file.transferTo(file2);
				brand.setPicPath(fileName);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return brandService.add(brand)>0;
	}
}
